package com.example.month11.indicator;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.recyclerview01.R;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 点击或切换的时候，改变顶部title的样式。当前的电量、上一个位置重置。
 * ViewPager指示器：Adapter适配器模式的应用。
 */
public class VpActivity extends AppCompatActivity {
    private String[] items = {"直播", "推荐", "视频", "奥运会", "世界杯", "段子", "图片", "推荐", "视频", "奥运会", "世界杯", "段子"};
    private ViewPager mViewPager;
    private TrackIndicatorView mIndicatorContainer;
    private List<ColorTrackTextView> mIndicators = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vp);

        mViewPager = findViewById(R.id.view_pager);
        mIndicatorContainer = findViewById(R.id.indicator_view);
        initIndicator();
        initViewPager();
    }

    private void initViewPager() {
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return ItemFragment.newInstance(items[position]);
            }

            @Override
            public int getCount() {
                return items.length;
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                ColorTrackTextView left = mIndicators.get(position);
//                left.setDirection(ColorTrackTextView.Direction.DIRECTION_RIGHT);
//                left.setCurrentProgress(1 - positionOffset);
//                try {
//                    ColorTrackTextView right = mIndicators.get(position + 1);
//                    right.setDirection(ColorTrackTextView.Direction.DIRECTION_LEFT);
//                    right.setCurrentProgress(positionOffset);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }

            @Override
            public void onPageSelected(int position) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        // 默认一进入就选中第一个
//        ColorTrackTextView left = mIndicators.get(0);
//        left.setDirection(ColorTrackTextView.Direction.DIRECTION_RIGHT);
//        left.setCurrentProgress(1);
    }

    private void initIndicator() {
        // 使用泛型，规范适配器的View。
//        mIndicatorContainer.setAdapter(new IndicatorAdapter<ColorTrackTextView>() {
        mIndicatorContainer.setAdapter(new IndicatorAdapter<TextView>() {
            @Override
            public int getCount() {
                return items.length;
            }

            @Override
            public TextView getView(int position, ViewGroup parent) {
                // 使用layout的形式，构建Tab的UI。eg: View.inflate(R.layout.item)
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
//                ColorTrackTextView item = new ColorTrackTextView(VpActivity.this);
                TextView item = new TextView(VpActivity.this);
//                item.setChangeColor(Color.BLACK);
                item.setTextColor(Color.BLACK);
                item.setTextSize(18);
                item.setGravity(Gravity.CENTER);
                item.setText(items[position]);
                item.setLayoutParams(params);
//                mIndicators.add(item); // 加入集合中
                return item;
            }

            @Override
            public void highLightIndicator(TextView view) { // 当前View: 选中高亮
//                view.setChangeColor(Color.RED);
                // view.setCurrentProgress(1);
                // view.setDirection(ColorTrackTextView.Direction.RIGHT_TO_LEFT);
                view.setTextColor(Color.RED);
            }
            @Override
            public void restoreIndicator(TextView view) { // 上一个View，不再高亮
                view.setTextColor(Color.BLACK);
//                ColorTrackTextView textView = (ColorTrackTextView) view;
//                view.setChangeColor(Color.BLACK); // view.setCurrentProgress(0);
                // view.setDirection(ColorTrackTextView.Direction.LEFT_TO_RIGHT);
            }
            @Override
            public View getBottomTrackView() {
                View view = new View(VpActivity.this);
                view.setBackgroundColor(Color.RED);
                // 指定底部指示器的宽度、高度
//                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 8));
                view.setLayoutParams(new ViewGroup.LayoutParams(80, 8));
                return view;
            }
        }, mViewPager, false);
    }

}

// TODO: 整理图片，笔记添加。
/**
 * http第三方网络引擎，缓存数据，没网络也能获取缓存的数据
 * 一键换肤，插件开发。
 * 增量更新
 * 动态修复，打补丁。
 *
 */